package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.PasswordConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.exception.AccountLockedException;
import com.sky.exception.AccountNotFoundException;
import com.sky.exception.PasswordErrorException;
import com.sky.mapper.EmployeeMapper;
import com.sky.result.PageResult;
import com.sky.service.EmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 员工登录
     *
     * @param employeeLoginDTO
     * @return
     */
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        String username = employeeLoginDTO.getUsername();//从前端输入框获取数据
        String password = employeeLoginDTO.getPassword();

        //1、根据用户名查询数据库中的数据
        Employee employee = employeeMapper.getByUsername(username);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (employee == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码比对
        // TODO 后期需要进行md5加密，然后再进行比对
        if (!password.equals(employee.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (employee.getStatus() == StatusConstant.DISABLE) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        //3、返回实体对象
        return employee;
    }

    @Override
    public void save(EmployeeDTO employeeDTO) {//新增员工

        System.out.println("当前线程的id:"+Thread.currentThread().getId());

        Employee employee=new Employee();

        //对象属性拷贝
        BeanUtils.copyProperties(employeeDTO,employee);//employeeDTO->employee 前提是属性名一致
        //设置账号状态,默认是能用的
        employee.setStatus(StatusConstant.ENABLE);
        //设置密码 默认为123456
        employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));
        //设置创建时间和修改时间
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
       // employeeMapper.save(employeeDTO);
        //设置当前记录的创建人id和修改人id
        //TODO

      //  employee.setCreateUser(10L);
        employee.setCreateUser(BaseContext.getCurrentId());//从线程中读取
        //  employee.setUpdateUser(10L);
        employee.setUpdateUser(BaseContext.getCurrentId());//从线程中读取
        employeeMapper.insert(employee);
       // return employee;
    }

    @Override
    public PageResult pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {//员工分页查询
        //开始分页查询
        System.out.println("分页2。。。。。。。。。。。");
        //此句不能省，是为了sql语句查询时从employeePageQueryDTO中获取参数，然后拼接limit 0,10这类的语句
    PageHelper.startPage(employeePageQueryDTO.getPage(),employeePageQueryDTO.getPageSize());//类似于mybatis的动态sql

        System.out.println("分页3头。。。。。。。。。。。。。。。。。。。");
        Page<Employee> page= employeeMapper.pageQuery(employeePageQueryDTO);
        System.out.println("分页3尾.。。。。。。。。。。。。。。");
    PageResult pageResult = new PageResult();

    pageResult.setTotal(page.getTotal());//设置总记录数
    pageResult.setRecords(page.getResult());//设置查到的员工集合数据
       return pageResult;
    }

    @Override
    public void startOrStop(Integer status, Long id) {//启用禁用员工账号
        //让Mapper执行update语句
//       Employee employee=new Employee();
//        employee.setStatus(status);
//        employee.setId(id);//或者使用Employee类中的builder注解功能
        Employee employee=Employee.builder()
                        .status(status)
                        .id(id)
                        .build();
       employeeMapper.update(employee);
    }

    @Override
    public Employee findEmployeeById(Long id) {//根据id查询员工
        Employee employee=employeeMapper.findEmployeeById(id);
        return employee;
    }

    @Override
    public void update1(Employee employee) {//编辑员工信息

      //  employee.setUpdateTime(LocalDateTime.now());//更新时间
      //  employee.setUpdateUser(BaseContext.getCurrentId());//更新信息的操作人
        employeeMapper.update1(employee);
    }

}
